<style>
    .notify {
        display: none;
    }
</style>

<div class="layui-layout layui-layout-admin" style="padding-left: 40px;margin-top: 20px;">
    <form class="layui-form" action="" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label mw200">任务名称</label>
            <div class="layui-input-inline mw400">
                <input type="text" name="task_name" id="task_name" lay-verify="required" autocomplete="off"
                       placeholder="任务名称" class="layui-input" value="">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label mw200">任务分组</label>
            <div class="layui-input-inline">
                <select name="group_id" lay-verify="required">
                {{range $k, $v := .taskGroup}}
                    <option value="{{$k}}">{{$v}}</option>
                {{end}}
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label mw200">执行资源</label>
            <div class="layui-input-inline mw400">
                <select name="server_id" lay-verify="required" lay-search xm-select="select_server" >
                    <option value="">请选择</option>
                    <option value="0">本机执行</option>
                    {{range $k, $v := .serverGroup}}
                        <optgroup label="{{$v.GroupName}}">
                            {{range $kk, $vv := $v.Servers}}
                                <option value="{{$kk}}">{{$vv}}</option>
                            {{end}}
                        </optgroup>
                    {{end}}
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">执行策略</label>
            <div class="layui-input-inline mw400">
                <input type="radio" name="server_type" value="0" lay-filter="server_type" title="同时执行" checked>
                <input type="radio" name="server_type" value="1" lay-filter="server_type" title="轮询执行">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label mw200">任务说明</label>
            <div class="layui-input-inline mw400">
                <textarea name="description" id="description" rows="2" placeholder="请输入内容"
                          class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">是否单例</label>
            <div class="layui-input-inline ">

                <input type="radio" name="concurrent" lay-verify="required" value="0" title="是" checked>
                <input type="radio" name="concurrent" lay-verify="required" value="1" title="否">

            </div>
            <div class="layui-form-mid layui-word-aux"><i class="fa fa-info-circle" aria-hidden="true" id="des"></i>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">时间表达式</label>
            <div class="layui-input-inline mw400">
                <input type="text" name="cron_spec" id="cron_spec" lay-verify="required" autocomplete="off"
                       placeholder="*/40 * * * * " class="layui-input" value="">
            </div>
            <div class="layui-form-mid layui-word-aux"><a href="/help" target="_blank"><i class="fa fa-question-circle"
                                                                                          aria-hidden="true"></i></a>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">命令脚本</label>
            <div class="layui-input-inline mw400">
                <textarea name="command" id="command" rows="5" placeholder="echo hello world && ls -a" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">超时设置</label>
            <div class="layui-input-inline">
                <input type="text" name="timeout" id="timeout" lay-verify="required" autocomplete="off"
                       placeholder="单位：秒，默认一天" class="layui-input" value="">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">出错通知</label>
            <div class="layui-input-inline ">
                <input type="radio" name="is_notify" lay-verify="required" value="1" lay-filter="is_notify" title="是">
                <input type="radio" name="is_notify" lay-verify="required" value="0" lay-filter="is_notify" title="否"
                       checked>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <div class="layui-form-item notify">
            <label class="layui-form-label mw200">通知类型</label>
            <div class="layui-input-inline mw400">
                <input type="radio" name="notify_type" value="0" lay-filter="notify_type" title="邮件">
                <input type="radio" name="notify_type" value="1" lay-filter="notify_type" title="短信">
                <input type="radio" name="notify_type" value="2" lay-filter="notify_type" title="钉钉">
                <input type="radio" name="notify_type" value="3" lay-filter="notify_type" title="微信">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <div class="layui-form-item notify">
            <label class="layui-form-label mw200">通知模板</label>
            <div class="layui-input-inline">
                <select name="notify_tpl_id" lay-filter="notify_tpl_id">
                    <option value="0" selected>请选择</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>


        <div class="layui-form-item notify">
            <label class="layui-form-label mw200">通知用户</label>
            <div class="layui-input-inline mw400">
                <select name="notify_user"  lay-search xm-select="select_notify" >
                    <option value="">请选择</option>
                    {{range $k, $v := .adminInfo}}
                        <option value="{{$v.Id}}">{{$v.RealName}}</option>
                    {{end}}
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <input type="hidden" name="id" id="id" value="0">
        <div class="layui-form-item">
            <label class="layui-form-label mw200"></label>
            <div class="layui-input-inline mw400">
                <button class="layui-btn" lay-submit="" lay-filter="sub">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script>

    layui.config({
       base:'/static/admin/js/'
    }).extend({
        formSelects: 'formSelects-v3'
    });
    layui.use(['form', 'element', 'layer', 'jquery','formSelects'], function () {
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        var $ = layui.jquery;
        var formSelects = layui.formSelects;

        formSelects.render({
            name : 'select_server',
        });

        formSelects.render({
            name : 'select_notify',
        });

        var error_info = "{{.flash.error}}";
        if (error_info) {
            layer.msg(error_info, {icon: 2, shade: 0.3}, function () {
                window.history.go(-1)
            })
            return;
        }
        $("#des").on('click', function () {
            layer.tips('设为“是”的话，如果该任务在上一个时间点还没执行完，则略过不执行', '#des', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
        });



        form.on('radio(is_notify)', function (data) {
            if (data.value == 1) {
                $(".notify").show()
            } else {
                $(".notify").hide()
            }
        });

        form.on('submit(sub)', function (data) {

            var form_data = data.field;

            //选择执行器
            var ids = formSelects.value('select_server');	//获取选中的
            if (ids.length<1){
                layer.msg("请选择执行资源");
                return false;
            }

            var server_arr = new Array();
            $.each(ids,function (k,v) {
                server_arr.push(v.val);
            })

            form_data.server_ids = server_arr.join(",");

            if (form_data.server_ids==="" || form_data.server_ids===null){
                layer.msg("请选择服务器资源");
                return false;
            }

            //选择通知人
            if (form_data.is_notify==1 ) {
                var ids = formSelects.value('select_notify');	//获取选中的
                if (ids.length<1){
                    layer.msg("请选择通知人");
                    return false;
                }

                var notify_arr = new Array();
                $.each(ids,function (k,v) {
                    notify_arr.push(v.val);
                })

                form_data.notify_user_ids = notify_arr.join(",");

                if (form_data.notify_user_ids==="" || form_data.notify_user_ids===null){
                    layer.msg("请选择通知人");
                    return false;
                }
            }


            $.post('{{urlfor "TaskController.AjaxSave"}}', form_data, function (out) {
                if (out.status == 0) {
                    layer.msg("操作成功", {icon: 1, shade: 0.3, time: 1000}, function () {
                        // window.history.go(-1)
                        window.parent.deleteCurrentTab();
                    })

                } else {
                    layer.msg(out.message)
                }
            }, "json");
            return false;
        });

        form.on('radio(notify_type)', function (data) {
            $.post('{{urlfor "TaskController.AjaxNotifyType"}}', {notify_type: data.value}, function (out) {
                if (out.code == 0) {
                    $('select[name="notify_tpl_id"]').empty();
                    $('<option value="0" selected>请选择</option>').appendTo('select[name="notify_tpl_id"]');
                    for (var i = 0; i < out.data.length; i++) {
                        $('<option value="' + out.data[i].id + '">' + out.data[i].tpl_name + '</option>').appendTo('select[name="notify_tpl_id"]');
                    }

                    form.render();
                } else {
                    layer.msg(out.message)
                }
            }, "json");
        });

        //但是，如果你的HTML是动态生成的，自动渲染就会失效
        //因此你需要在相应的地方，执行下述方法来手动渲染，跟这类似的还有 element.init();
        form.render();
    });
</script>